package com.siwei.mes.entity.system;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.AppGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.enums.GenerateCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;


/**
 * 用户表(User)实体类
 *
 * @author linzi
 * @since 2023-11-25 18:16:11
 */
@Data
@Table(name = "pk_user")
@ApiModel(value = "User", description = "用户表")
@Accessors(chain = true)
public class User implements Serializable {
    private static final long serialVersionUID = 984632670482066158L;
    /**
     * 主键id
     */
    @Id
    @Column(name = "id")
    @ApiModelProperty(value = "主键id", name = "id")
    private Long id;
    /**
     * 用户编号
     */
    @Column(name = "user_no")
    @ApiModelProperty(value = "用户编号", name = "userNo")
    private String userNo;
    /**
     * 用户名称
     */
    @Column(name = "user_name")
    @ApiModelProperty(value = "用户名称", name = "userName")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class, SyncGroup.class}, message = "用户名称为空")
    private String userName;
    /**
     * 用户手机号
     */
    @Column(name = "user_phone")
    @ApiModelProperty(value = "用户手机号", name = "userPhone")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class, SyncGroup.class}, message = "用户手机号为空")
    @Size(max = 11, min = 11, message = "用户手机号不正确", groups = {AddGroup.class, UpdateGroup.class, SyncGroup.class})
    @Pattern(regexp = "^1[3456789]\\d{9}$", message = "用户手机号不正确", groups = {AddGroup.class, UpdateGroup.class})
    private String userPhone;
    /**
     * 密码
     */
    @Column(name = "password")
    @ApiModelProperty(value = "密码", name = "password")
    @NotBlank(groups = {AppGroup.class}, message = "用户密码为空")
    private String password;

    @Column(name = "role_id")
    @ApiModelProperty(value = "角色id")
    @NotNull(groups = {AddGroup.class, UpdateGroup.class}, message = "角色id为空")
    private Long roleId;

    @Column(name = "role_name")
    @ApiModelProperty(value = "角色名称")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "角色名称为空")
    private String roleName;
    /**
     * 职位
     */
    @Column(name = "job")
    @ApiModelProperty(value = "职位", name = "job")
    private String job;
    /**
     * 头像
     */
    @Column(name = "avatar")
    @ApiModelProperty(value = "头像", name = "avatar")
    private String avatar;

    @Column(name = "user_sign_img")
    @ApiModelProperty(value = "用户签名图片")
    private String userSignImg;

    /**
     * 试验项目json
     */
    @Column(name = "test_project_json")
    @ApiModelProperty(value = "试验项目json", name = "testProjectJson")
    private JSONArray testProjectJson;
    /**
     * 状态  1-正常 0-作废
     */
    @Column(name = "status")
    @ApiModelProperty(value = "状态  1-正常 0-作废", name = "status")
    private Integer status;
    /**
     * 创建人
     */
    @Column(name = "creater")
    @ApiModelProperty(value = "创建人", name = "creater")
    private Long creater;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @ApiModelProperty(value = "创建时间", name = "createTime")
    private Date createTime;
    /**
     * 修改人
     */
    @Column(name = "updater")
    @ApiModelProperty(value = "修改人", name = "updater")
    private Long updater;
    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @ApiModelProperty(value = "更新时间", name = "updateTime")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;
    /**
     * 是否删除
     */
    @Column(name = "is_del")
    @ApiModelProperty(value = "是否删除", name = "isDel")
    private Integer isDel;

    @Column(name = "sync_id")
    @ApiModelProperty(value = "第三方id")
    @NotBlank(groups = {SyncGroup.class}, message = "第三方id为空")
    private String syncId;

    @Column(name = "sync_type")
    @ApiModelProperty(value = "第三方类型")
    private String syncType;

    @Column(name = "society_user_name")
    @ApiModelProperty(value = "协会用户名")
    private String societyUserName;

    @Column(name = "id_card")
    @ApiModelProperty(value = "'身份证'")
    //@NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "身份证为空")
    //@Pattern(regexp = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)", message = "身份证不正确", groups = {AddGroup.class, UpdateGroup.class})
    private String idCard;

    @Column(name = "age")
    @ApiModelProperty(value = "年龄")
    private Integer age;

    @Column(name = "education_code")
    @ApiModelProperty(value = "学历code  字典EDUCATION_TYPE")
    private String educationCode;

    @Column(name = "education_name")
    @ApiModelProperty(value = "学历名称")
    private String educationName;

    @Column(name = "speciality")
    @ApiModelProperty(value = "专业")
    private String speciality;

    @Column(name = "technical_titles")
    @ApiModelProperty(value = "技术职称")
    private String technicalTitles;

    @Transient
    @ApiModelProperty(value = "工单状态 0空闲 1忙碌")
    private String orderType;

    @Transient
    @ApiModelProperty(value = "是否工程区域划分 0否 1是")
    private String isArea;

    @Transient
    @ApiModelProperty(value = "用户ID数组")
    private List<String> ids;

    @Transient
    @ApiModelProperty(value = "工程区域名称")
    private String projectArea;

    /**
     * 原密码
     */
    @Transient
    @ApiModelProperty(value = "原密码")
    @NotBlank(groups = {AppGroup.class}, message = "用户原密码为空")
    private String oldPassword;

    public User init(boolean isCreate) {
        this.setUpdater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null)
                .setUpdateTime(new Date());
        if (isCreate) {
            this.setCreater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null)
                    .setCreateTime(new Date())
                    .setStatus(1)
                    .setIsDel(0).setUserNo(UidUtils.genNo(GenerateCodeEnum.USER.getCode()));
        }
        return this;
    }
}
